<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
    <script src="lib/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="lib/json2.js" type="text/javascript"></script>
    <script src="lib/jsmart.js" type="text/javascript"></script>
    <script src="script/app.js" type="text/javascript"></script>
    <script src="script/urledit.js" type="text/javascript"></script>
        
    <script id="suppliercount" type="text/x-jsmart-tmpl">
	搜索列表：共{$hits.total}条记录
    </script>
    <!--采集报价-->
    <script id="supplier" type="text/x-jsmart-tmpl">  
	   	<tr>
    	  	<th>编号</th>
           	<th>供应商名称</th>
           	<th>联系人</th>
          	<th>联系方式</th>
           	<th>主营产品</th>
           	<th>报价单</th>
           	<th>更新时间</th>
           	<th>采集人员</th>
           	<th>备注</th>
        </tr>
		{foreach $hits.hits as $name => $supplier}
            <tr>
           		  	<td>{$supplier._source.supplierid}</td>
                	<td>{$supplier._source.companyname}</td>
                	<td>{$supplier._source.contacts[0].name}</td>
                	<td>{$supplier._source.contacts[0].telephone}</td>
                	<td>{$supplier._source.mainproduct}</td>
                	<td><a href="supplierproduct.html?id={$supplier._source.supplierid}&name={$supplier._source.companyname}" target="_blank">查看</a></td>
                	<td>{$supplier._source.update_at}</td>
                </tr>
		{/foreach}
	</script>
 
     <!--历史记录-->
    <script id="history" type="text/x-jsmart-tmpl">  
	   	<tr>
    	  	<th>询价单号</th>
           	<th>询价时间</th>
           	<th>材料名称</th>
          	<th>规格型号</th>
           	<th>品牌</th>
           	<th>单位</th>
           	<th>价格</th>
           	<th>供应商名称</th>
           	<th>联系人</th>
           	<th>联系方式</th>
           	<th>备注</th>
        </tr>
		{foreach $hits.hits as $name => $supplier}
            <tr>
           		  	<td>{$supplier._source.askid}</td>
                	<td>{$supplier._source.date}</td>
                	<td>{$supplier._source.name}</td>
                	<td>{$supplier._source.spec}</td>
                	<td>{$supplier._source.brand}</td>
                	<td>{$supplier._source.unit}</td>
                	<td>{$supplier._source.price}</td>
                	<td>{$supplier._source.supplier}</td>
                	<td>{$supplier._source.contact}</td>
                	<td>{$supplier._source.phone}</td>
                	<td>{$supplier._source.remark}</td>
                </tr>
		{/foreach}
	</script>   

     <!--定额信息-->
    <script id="quota" type="text/x-jsmart-tmpl">  
	   	<tr>
    	  	<th>类型</th>
           	<th>材料名称</th>
          	<th>规格型号</th>
           	<th>单位</th>
           	<th>价格</th>
           	<th>年份</th>
           	<th>月份</th>
           	<th>序列号</th>
        </tr>
		{foreach $hits.hits as $name => $supplier}
            <tr>
           		  	<td>{$supplier._source.kind}</td>
                	<td>{$supplier._source.name}</td>
                	<td>{$supplier._source.spec}</td>
                	<td>{$supplier._source.unit}</td>
                	<td>{$supplier._source.price}</td>
                	<td>{$supplier._source.year}</td>
                	<td>{$supplier._source.month}</td>
                	<td>{$supplier._source.serialnum}</td>
                </tr>
		{/foreach}
	</script>   
        

     <!--辅材信息-->
    <script id="help" type="text/x-jsmart-tmpl">  
	   	<tr>
    	  	<th>工料机编码</th>
           	<th>工料机名称</th>
          	<th>工料机规格</th>
           	<th>单位</th>
           	<th>年份</th>
           	<th>月份</th>
           	<th>当月价格</th>
           	<th>上月价格</th>
           	<th>增减值</th>
           	<th>增减率</th>
        </tr>
		{foreach $hits.hits as $name => $supplier}
            <tr>
           		  	<td>{$supplier._source.code}</td>
                	<td>{$supplier._source.name}</td>
                	<td>{$supplier._source.spec}</td>
                	<td>{$supplier._source.unit}</td>
                	<td>{$supplier._source.year}</td>
                	<td>{$supplier._source.month}</td>
                	<td>{changeTwoDecimal($supplier._source.price)}</td>
                	<td>{changeTwoDecimal($supplier._source.previousprice)}</td>
                	<td>{$supplier._source.change}</td>
                	<td>{$supplier._source.scale}</td>
                </tr>
		{/foreach}
	</script>   
    
     <!--全站-->
    <script id="all" type="text/x-jsmart-tmpl">  
	   	<tr>
    	  	<th>时间</th>
           	<th>材料名称</th>
           	<th>规格参数</th>
          	<th>单位</th>
           	<th>价格</th>
           	<th>供应商名称</th>
           	<th>联系人</th>
           	<th>联系方式</th>
           	<th>来源</th>
        </tr>
		{foreach $hits.hits as $name => $supplier}
            <tr>
           		  	<td>{$supplier._source.update_at}{$supplier._source.date}{YpM($supplier._source.year,$supplier._source.month)}</td>
                	<td>{$supplier._source.name}</td>
                	<td>{$supplier._source.spec}</td>
                	<td>{$supplier._source.unit}</td>
                	<td>{$supplier._source.prices[0].price}</td>
                	<td>{$supplier._source.suppliername}{$supplier._source.companyname}{$supplier._source.supplier}</td>
                	<td>{$supplier._source.contact}{$supplier._source.contacts[0].name}</td>
                	<td>{$supplier._source.phone}{$supplier._source.contacts[0].telephone}</td>
                </tr>
		{/foreach}
	</script>               
    <!--script id="supplier" type="text/x-jsmart-tmpl">  
	{foreach $hits.hits as $name => $supplier}
		<li>
			<div class="fl gysqy_lb_tp">
				<img src="resources/img/gysqy_pic01.jpg" width="122" height="120" /></div>
			<div class="fl gysqy_lb_nr">
				<div class="gysqy_lb_title">
					<a style="_line-height: 18px;" href="/Manufacturer/Index?mId=8176" title="{$supplier._source.companyname}"
						target="_blank">{$supplier._source.companyname}</a></div>
				<div class="gysqy_lb_wz">
				</div>
				<div class="gysqy_lb_lx" title="类型：销售商  主营产品: 水泵、风机">
					类型：
					{if $supplier._source.doctype == "supplier"}
						供应商
					{/if}
				</div>
				<div class="gysqy_lb_dz">
					地址：{$supplier._source.companyaddress}</div>
			</div>
			<div class="fl gysqy_lb_an">
				<a href="/Manufacturer/Index?mId=8176" title="{$supplier._source.companyname}" target="_blank">
					<img src="resources/img/lxsj.png" width="92" height="28" border="0" /></a>
			</div>
		</li>
	{/foreach}
    </script-->
    <script id="supplierpage" type="text/x-jsmart-tmpl">
	<div class="page">
		<div class="pagination">
			{if $pagecount > 1}			
				<a class="pageNum"><b>
				{$currentpage}
				</b>/<b>
				{$pagecount}
				</b></a>	
				{if (1 != $currentpage)}
					<a href="{urlset('pageIndex', 1)}" class="first">首页</a>
					<a href="{urlset('pageIndex', $currentpage-1)}" class="prev">上一页</a>
				{/if}	
				
				{for $i=$start to $end}
					{if ($i == $currentpage)}
						<span class="current">{$i}</span>
					{else}
						<a href="{urlset('pageIndex', $i)}">{$i}</a>
					{/if}
				{/for}
				
				{if ($pagecount != $currentpage)}
					<a href="{urlset('pageIndex', $currentpage+1)}" class="first">下一页</a>
					<a href="{urlset('pageIndex', $pagecount)}" class="last">尾页</a>
				{/if}	
			{/if}
		</div>
	</div>
	</script>
</head>
<body>
    <center>
    	<div id="search" style="margin-bottom:10px;">
       	  <input type="radio" value="supplier" name="searchtype" checked="true"/>采集报价
       	  <input type="radio" value="history" name="searchtype" />历史记录
       	  <input type="radio" value="quota" name="searchtype" />定额信息
       	  <input type="radio" value="help" name="searchtype" />辅材信息
       	  <input type="radio" value="none" name="searchtype" />建材与造价咨询
            <br />
          <input type="text" id="searchvalue" style="width:300px" />
            <input type="button" value="全站" id="all"/>
            <input type="button" value="精确" id="accurate"/>         
        </div>
        <div id="content">
            <style>
				table{border:1 solid;border-width 1 0 0 1;}
				#searchresult tr th{border:1 solid;}
    		</style>            
            <div id="count">
            </div>
        	<table cellpadding="10" cellspacing="0" border="1" id="table">
 
            </table>
      </div>      
      <div id="page">
      </div>
      <script type="text/javascript">
		  function isCon(arr, val){
			for(var i=0; i<arr.length; i++){
				if(arr[i] == val)
					return true;
			}
			return false;
		}
		
        $(document).ready(
			function () {								
				var myurl=new UrlEdit.URL(document.URL);
				var value = "*"	;
				var skip = 0;
				var pagesize = 10;
				if(myurl.get("pageIndex") != undefined)
					currentpage = myurl.get("pageIndex");
				if(myurl.get("pagesize") != undefined)
					pagesize = myurl.get("pagesize");
				if(myurl.get("searchvalue") != undefined && myurl.get("searchvalue") != "")
				{
					//values = myurl.get("searchvalue").split(' ');
					$("#searchvalue").attr("value", decodeURI(myurl.get("searchvalue")));
					value = myurl.get("searchvalue");
				}
					
				var skip = pagesize * (currentpage - 1);
				var pageparam = "?from=" + skip + "&size=" + pagesize;					
				//for (ind in values)
			//	{
				//	pageparam = pageparam + "&q=companyname:\"" + values[ind] + "\""
				//}
				var db = myurl.get("db");
				var searchdb = "/material/"
				if (db != undefined && db != "all" && db != "")
				{
					searchdb = searchdb + db + "/_search?pretty=true"
					var temp = $("input[type='radio']");
					$(":radio").attr("checked", db);
					$(":radio").each(function()
						{
							if ($(this).val() == db)
								{
									this.checked = true;
								}
						}
					);
				}
				else
				{
					searchdb = searchdb + "_search?pretty=true"
				}
								
				var params = new Object();
                params["from"] = skip;
				params["size"] = pagesize;
				params["query"] = new Object();
				params["query"]["filtered"] = new Object();
				params["query"]["filtered"]["query"] = new Object();
				params["query"]["filtered"]["query"]["query_string"] = new Object();
				params["query"]["filtered"]["query"]["query_string"]["fields"] = new Array();
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("supplierid");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("companyname");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("update_at");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("name");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("telephone");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("askid");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("date");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("spec");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("brand");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("unit");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("price");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("supplier");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("contact");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("phone");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("remark");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("kind");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("year");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("month");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("serialnum");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("code");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("previousprice");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("change");
				params["query"]["filtered"]["query"]["query_string"]["fields"].push("scale");
				params["query"]["filtered"]["query"]["query_string"]["query"] =decodeURI(value);
				params["query"]["filtered"]["query"]["query_string"]["default_operator"] = "AND";
				params["query"]["filtered"]["filter"] = new Object();
				if(db == "all")
				{
					params["query"]["filtered"]["filter"]["not"] = new Object();
					params["query"]["filtered"]["filter"]["not"]["term"] = new Object();
					params["query"]["filtered"]["filter"]["not"]["term"]["doctype"] = "supplier";
				}
				else
				{					
					params["query"]["filtered"]["filter"]["match_all"] = new Object();
				}
								
			    $.ajax({
			        url: searchpath + searchdb,
			        type: 'POST',	
                    dataType: 'json',
                    data: JSON.stringify(params),
			        success: function (data) {
						var supplierids = new Array();
						for (var index in data.hits.hits)
						{
							if (data.hits.hits[index]._source.doctype == "product")
							{
								if(!isCon(supplierids, data.hits.hits[index]._source.supplierid))
								{
									supplierids.push(data.hits.hits[index]._source.supplierid);
								}
							}
						}
						if(supplierids.length > 0)
						{
							var firstdata = data;
							var searchparams = new Object();
							searchparams["from"] = 0;
							searchparams["size"] = pagesize;
							searchparams["query"] = new Object();
							searchparams["query"]["filtered"] = new Object();
							searchparams["query"]["filtered"]["query"] = new Object();
							searchparams["query"]["filtered"]["query"]["query_string"] = new Object();
							searchparams["query"]["filtered"]["query"]["query_string"]["fields"] = new Array();
							searchparams["query"]["filtered"]["query"]["query_string"]["fields"].push("companyname");
							searchparams["query"]["filtered"]["query"]["query_string"]["fields"].push("companyaddress");
							searchparams["query"]["filtered"]["query"]["query_string"]["query"] = "*";
							searchparams["query"]["filtered"]["query"]["query_string"]["default_operator"] = "AND";			
							searchparams["query"]["filtered"]["filter"] = new Object();					
							searchparams["query"]["filtered"]["filter"]["terms"] = new Object();		
							searchparams["query"]["filtered"]["filter"]["terms"]["supplierid"] = supplierids;
							$.ajax({
								url: searchpath + "/material/supplier/_search",
								type: 'POST',	
								dataType: 'json',
								data: JSON.stringify(searchparams),
								success: function (data) {
									for (var index in firstdata.hits.hits)
									{
										if (firstdata.hits.hits[index]._source.doctype == "product")
										{
											var id = firstdata.hits.hits[index]._source.supplierid;
											for (var index1 in data.hits.hits)
											{
												if(firstdata.hits.hits[index]._source.supplierid == data.hits.hits[index1]._source.supplierid)
												{
													firstdata.hits.hits[index]._source.contact = data.hits.hits[index1]._source.contacts[0].name;
													firstdata.hits.hits[index]._source.phone = data.hits.hits[index1]._source.contacts[0].mobile;
												}
											}
										}
									}
									
									var tpl;
									if(db == "supplier")
									{
										tpl = new jSmart(document.getElementById('supplier').innerHTML);
									}
									else if(db == "history")
									{							
										tpl = new jSmart(document.getElementById('history').innerHTML);	
									}
									else if(db == "quota")
									{							
										tpl = new jSmart(document.getElementById('quota').innerHTML);	
									}
									else if(db == "help")
									{							
										tpl = new jSmart(document.getElementById('help').innerHTML);	
									}
									else if(db == "all")
									{							
										tpl = new jSmart(document.getElementById('all').innerHTML);	
									}
									var res = tpl.fetch(firstdata);
									$("#table").html(res);	
								}
							});
						}
						else
						{												
							var tpl;
							if(db == "supplier")
							{
								tpl = new jSmart(document.getElementById('supplier').innerHTML);
							}
							else if(db == "history")
							{							
								tpl = new jSmart(document.getElementById('history').innerHTML);	
							}
							else if(db == "quota")
							{							
								tpl = new jSmart(document.getElementById('quota').innerHTML);	
							}
							else if(db == "help")
							{							
								tpl = new jSmart(document.getElementById('help').innerHTML);	
							}
							else if(db == "all")
							{							
								tpl = new jSmart(document.getElementById('all').innerHTML);	
							}
							var res = tpl.fetch(data);
							$("#table").html(res);	
						}	
						
						
							
						page = new Array();
						page.currentpage = Number(currentpage);
						page.pagecount = Math.ceil(Number(data.hits.total) / Number(pagesize));
						page.start = 1;
						page.end = 9;
						if (Number(page.pagecount) > 9)
						{
							if (Number(currentpage) > 4)
							{
								if (Number(page.pagecount) - Number(currentpage) > 4)
								{
									page.start = Number(currentpage) - 4;
									page.end = Number(currentpage) + 4;
								}
								else
								{
									page.end = Number(page.pagecount);
									page.start = Number(page.end) - 8;
								}
							}
						
						}
						else
						{
							page.end = page.pagecount;
						}
						tpl = new jSmart(document.getElementById('supplierpage').innerHTML);
						res = tpl.fetch(page);
						$("#page").html(res);			
						
						tpl = new jSmart(document.getElementById('suppliercount').innerHTML);
						var res = tpl.fetch(data);
						$("#count").html(res);	
			        }
			    });
				
				$("#all").live("click", function(){
					var item = $('input[@name="searchtype"]:checked').val();
					var searchvalue = $("#searchvalue").val();
					searchparams = "?searchvalue=" + searchvalue + "&db=all";
					myurl.remove("pageIndex");
					myurl.remove("pagesize");
					myurl.remove("searchvalue");
					myurl.remove("db");
					window.location.href = myurl.url() + searchparams;
				});
				
				$("#accurate").live("click", function(){
					var item = $('input[@name="searchtype"]:checked').val();
					var searchvalue = $("#searchvalue").val();
					if(item == "none")
					{
						alert("库未提供");
						return;
					}
					searchparams = "?searchvalue=" + searchvalue + "&db=" + item;
					myurl.remove("pageIndex");
					myurl.remove("pagesize");
					myurl.remove("searchvalue");
					myurl.remove("db");
					window.location.href = myurl.url() + searchparams;
				});
				
				$("#new").live("click", function(){
					var item = $('input[@name="searchtype"]:checked').val();
					var searchvalue = $("#searchvalue").val();
					alert (item + ":" + searchvalue);
					//searchparams = "?searchvalue=" + searchvalue;
					//myurl.remove("pageIndex");
					//myurl.remove("pagesize");
					//myurl.remove("searchvalue");
					//window.location.href = myurl.url() + searchparams;
				});
			}
		);
        </script>
    </center>
</body>
</html>
